<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\model\BranchCategoryModel;
use app\goods\model\GoodsCategoryModel;
use think\Db;
use app\admin\model\ThemeModel;
use tree\Tree;

class ReleaseController extends AdminBaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 全站发布
     * @adminMenu(
     *     'name'   => '全站发布',
     *     'parent' => 'admin/Release/index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '全站发布',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        /*文章分类结构*/
        $tree       = new Tree();
        $tree->icon = ['│ ', '├─ ', '└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';

        $where = ['delete_time' => 0];
        $portalCategoryModel = new PortalCategoryModel();
        $result = $portalCategoryModel
               -> where($where)
               -> order("list_order", "ASC")
               -> column('');

        foreach ($result as $n => $t) {
            $result[$n]['checked']      = ($t['is_show']==1) ? ' checked' : '';
            $result[$n]['level']        = $this->_getLevel($t['id'], $result);
            $result[$n]['style']        = empty($t['parent_id']) ? '' : 'display:none;';
            $result[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-' . $t['parent_id'] . '"' : '';
        }

        $str = "<tr id='node-\$id'\$parentIdNode  style='\$style'>
                   <td style='padding-left:30px;'>\$spacer<label><input type='checkbox' name='ids[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</label></td>
                </tr>";
        $tree->init($result);

        $category = $tree->getTree(0, $str);
        $this->assign("category", $category);




        /*基地分类结构*/
        $where_b = ['delete_time' => 0];
        $branchCategoryModel = new BranchCategoryModel();
        $result_b = $branchCategoryModel
               -> where($where_b)
               -> order("list_order", "ASC")
               -> column('');

        foreach ($result_b as $n => $t) {
            $result_b[$n]['checked']      = ($t['is_show']==1) ? ' checked' : '';
            $result_b[$n]['level']        = $this->_getLevel($t['id'], $result_b);
            $result_b[$n]['style']        = empty($t['parent_id']) ? '' : 'display:none;';
            $result_b[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-b' . $t['parent_id'] . '"' : '';
        }

        $str_b = "<tr id='node-b\$id'\$parentIdNode  style='\$style'>
                   <td style='padding-left:30px;'>\$spacer<label><input type='checkbox' name='ids_b[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</label></td>
                </tr>";
        $tree->init($result_b);

        $category_b = $tree->getTree(0, $str_b);
        $this->assign("category_b", $category_b);


        /*产品分类结构*/
        $where_g = ['delete_time' => 0];
        $goodsCategoryModel = new GoodsCategoryModel();
        $result_g = $goodsCategoryModel
               -> where($where_g)
               -> order("list_order", "ASC")
               -> column('');

        foreach ($result_g as $n => $t) {
            $result_g[$n]['checked']      = ($t['is_show']==1) ? ' checked' : '';
            $result_g[$n]['level']        = $this->_getLevel($t['id'], $result_g);
            $result_g[$n]['style']        = empty($t['parent_id']) ? '' : 'display:none;';
            $result_g[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-g' . $t['parent_id'] . '"' : '';
        }

        $str_g = "<tr id='node-g\$id'\$parentIdNode  style='\$style'>
                   <td style='padding-left:30px;'>\$spacer<label><input type='checkbox' name='ids_g[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</label></td>
                </tr>";
        $tree->init($result_g);

        $category_g = $tree->getTree(0, $str_g);

        $this->assign("category_g", $category_g);



        return $this->fetch();
    }


    /**
     * 设置要发布的栏目
     * @adminMenu(
     *     'name'   => '设置要发布的栏目',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '设置要发布的栏目',
     *     'param'  => ''
     * )
     */
    public function showPost()
    {
        if ($this->request->isPost()) {
                $where      = ['delete_time' => 0];
                $where2     = ['status' => 1];
                $where_b    = ['delete_time' => 0];
                $where_b2   = ['status' => 1];
                $where_g    = ['delete_time' => 0];
                $where_g2   = ['status' => 1];
                $ids        = $this->request->param('ids/a');
                $ids_b      = $this->request->param('ids_b/a');
                $ids_g      = $this->request->param('ids_g/a');

                
                /*设置文章栏目是否显示*/
                db('portal_category')->where($where)->update(['is_show'=>0]);
                $where['id'] = ['in',$ids];
                db('portal_category')->where($where)->update(['is_show'=>1]);
                /*设置文章是否显示*/
                db('portal_category_post')->where($where2)->update(['is_show'=>0]);
                $where2['category_id'] = ['in',$ids];
                db('portal_category_post')->where($where2)->update(['is_show'=>1]);
                /*设置产品栏目是否显示*/
                db('goods_category')->where($where_g)->update(['is_show'=>0]);
                $where_g['id'] = ['in',$ids_g];
                db('goods_category')->where($where_g)->update(['is_show'=>1]);
                /*设置产品是否显示*/
                db('goods_category_relate')->where($where_g2)->update(['is_show'=>0]);
                $where_g2['category_id'] = ['in',$ids_g];
                db('goods_category_relate')->where($where_g2)->update(['is_show'=>1]);
                /*设置基地栏目是否显示*/
                db('branch_category')->where($where_b)->update(['is_show'=>0]);
                $where_b['id'] = ['in',$ids_b];
                db('branch_category')->where($where_b)->update(['is_show'=>1]);
                /*设置基地是否显示*/
                db('branch_category_post')->where($where_b2)->update(['is_show'=>0]);
                $where_b2['category_id'] = ['in',$ids_b];
                db('branch_category_post')->where($where_b2)->update(['is_show'=>1]);

                admin_log('','SETTING','NEED_PORTAL_CATEGORY');
            // }

            $this->success("保存成功！");
        }
    }

    /**
     * 获取栏目深度
     * @param $id
     * @param array $array
     * @param int $i
     * @return int
     */
    protected function _getLevel($id, $array = [], $i = 0)
    {   
        if ($array[$id]['parent_id'] == 0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id'] == $id) {
            return $i;
        } else {
       
            $i++;
          
            return $this->_getLevel($array[$id]['parent_id'], $array, $i);
        }
    }


}